package com.example.qrapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.DialogTitle;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;



public class MainActivity extends AppCompatActivity  implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    public Context mContext;
    Button button1;
    EditText editText1;
    TextView textview1;

    private boolean isHandleKey = true;

    ScannerGunManager scannerGunManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = MainActivity.this;

        button1 = findViewById(R.id.button1);
        button1.setOnClickListener(this);
        editText1 = findViewById(R.id.edittext1);
        editText1.setFocusable(false);
        textview1 = findViewById(R.id.textview1);

        scannerGunManager = new ScannerGunManager(new ScannerGunManager.OnScanListener() {
            @Override
            public void onResult(String code) {
                Log.d(TAG, "code= " + code);
                textview1.setText(code);
                //editText1.setText(code);//这里会自动弹出键盘
            }
        });


    }

    public void checkUSB() {
        UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();

        for (UsbDevice device : usbDevices.values()) {

            String deviceName = device.getDeviceName();
            int vebdroID = device.getVendorId();
            int productID = device.getProductId();
            Log.d(TAG, "deviceName=" + deviceName);
            Log.d(TAG, "vebdroID=" + vebdroID);
            Log.d(TAG, "productID=" + productID);

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                //editText1.setText("");
                textview1.setText("");
                break;
        }
    }

    @Override   // 拦截物理键盘事件
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.d(TAG, "event= " + event);

        if (scannerGunManager.dispatchKeyEvent(event)) {
            return true;
        }

        return super.dispatchKeyEvent(event);
    }
}
